/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MODEL.DAO;

import MODEL.POJO.LoaiDanhMucPOJO;
import MODEL.UTIL.MySqlDataAccessHelper;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author kijoo
 */
public class LoaiDanhMucDAO {

    public static ArrayList<LoaiDanhMucPOJO> LayDanhSachLoaiDanhMuc(String idloaisanpham) {
        ArrayList<LoaiDanhMucPOJO> ds = new ArrayList<LoaiDanhMucPOJO>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            String sql = String.format("SELECT * FROM LOAIDANHMUC WHERE IDLOAISANPHAM='%s'", idloaisanpham);
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                LoaiDanhMucPOJO ldm = new LoaiDanhMucPOJO();
                ldm.setIdloaidanhmuc(rs.getString("idloaidanhmuc"));
                ldm.setTenloaidanhmuc(rs.getString("tenloaidanhmuc"));
                ds.add(ldm);
            }
            helper.Close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return ds;
    }

    public static LoaiDanhMucPOJO LayLoaiDM(String idloaidanhmuc) {
        LoaiDanhMucPOJO ldm=new LoaiDanhMucPOJO();
        String sql = String.format("SELECT * FROM LOAIDANHMUC WHERE IDLOAIDANHMUC='%s' ", idloaidanhmuc);
                try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.executeQuery(sql);
            if (rs.next()) {
                ldm.setTenloaidanhmuc(rs.getString("tenloaidanhmuc"));
                ldm.setIdloaidanhmuc(rs.getString("idloaidanhmuc"));
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return ldm;
    }
    
        public static LoaiDanhMucPOJO LayLoaiDMTheoSP(String idsanpham) {
        LoaiDanhMucPOJO ldm = new LoaiDanhMucPOJO();
        String sql = String.format("SELECT LDM.TENLOAIDANHMUC,LDM.IDLOAIDANHMUC "
                + " FROM LOAIDANHMUC LDM,DANHMUC DM,SANPHAM SP "
                + " WHERE SP.IDSANPHAM='%s'AND SP.IDDANHMUC=DM.IDDANHMUC AND DM.IDLOAIDANHMUC=LDM.IDLOAIDANHMUC ", idsanpham);
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.executeQuery(sql);
            if (rs.next()) {
                ldm.setTenloaidanhmuc(rs.getString("TENLOAIDANHMUC"));
                ldm.setIdloaidanhmuc(rs.getString("IDLOAIDANHMUC"));
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return ldm;
    }
        
    public static boolean ThemLoaiDanhMuc(String maLoaiDM, String tenLoaiDM, String maLoaiSP) {
        boolean kq = false;
        try {
            String sql = String.format("INSERT INTO LOAIDANHMUC (IDLOAIDANHMUC,IDLOAISANPHAM,TENLOAIDANHMUC) VALUES('%s','%s','%s')", maLoaiDM, maLoaiSP, tenLoaiDM);
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            int n = helper.executeUpdate(sql);
            if (n == 1) {
                kq = true;
            }
            helper.Close();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return kq;

    }
}
